package com.itic.mall.mobile.goods.spu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.NumericHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.goods.goodssku.persistence.model.GoodsSku;
import com.itic.mall.system.goods.goodssku.service.GoodsSkuService;
import com.itic.mall.system.goods.goodsspu.persistence.model.GoodsSpu;
import com.itic.mall.system.goods.spufeedback.persistence.model.SpuFeedback;
import com.itic.mall.system.goods.spufeedback.persistence.model.SpuFeedbackExample;
import com.itic.mall.system.goods.spufeedback.persistence.model.SpuFeedbackExample.Criteria;
import com.itic.mall.system.goods.spufeedback.service.SpuFeedbackService;
import com.itic.mall.system.orders.detail.service.OrdersDetailService;
import com.itic.mall.system.orders.item.service.OrdersItemService;
import com.itic.mall.system.users.usersmall.persistence.model.UsersMall;

@Controller
@RequestMapping(value = "/mobile/goods/skuFeedback")
public class SpuFeedbackInterface extends BaseMobileInterface{
	
	// 返回属性黑名单
	private static String[] ignoreProperties = new String[] {};
	
	@Autowired
	private SpuFeedbackService puFeedbackService;
	
	@Autowired
	private GoodsSkuService goodsSkuService;
	@Autowired
	private OrdersItemService ordersItemService;
	@Autowired
	private OrdersDetailService ordersDetailService;
	
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(SpuFeedback spuFeedback, HttpServletRequest request) {
		// 声明返回对象
		Map<String, Object> resultMap = new HashMap<String, Object>();
		SpuFeedbackExample example = this.queryCondition(request, spuFeedback);
		// 封装分页参数
		example = this.handlePageQuery(request, example);
		// 获取信息总条数
		int total = puFeedbackService.count(example);
		// 获取数据
		List<SpuFeedback> list = puFeedbackService.list(example);
		// 结果集
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		if (list.size() > 0) {
			// 过滤黑名单字段
			resultList = ValueHelper.tranList2MapList(list, ignoreProperties);
		}
		// 数据返回处理
		resultMap.put(IticConst.TOTAL_RECORDS, total);
		resultMap.put(IticConst.DISPLAY_RECORDS, total);
		resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
		resultMap.put(IticConst.DATA, resultList);
		return resultMap;
	}
	
	
	/**
	 * 添加模板管理数据
	 * 
	 * @param android
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, String> add(HttpServletRequest request, HttpServletResponse response, String token,String appId,
			int reorder ,String skuId,String content,String images,String overallLevel,String wuliuLevel,String descLevel,String serverLevel,String orderNo) {
		
		Map<String, String> resultMap = new HashMap<String, String>();
		
		String errorCode = PARAM_ERROR_CODE;
		String errorMsg = PARAM_ERROR_MSG;

		SpuFeedback spuFeedback  = new SpuFeedback();
		
		try{
			String result = FAIL;
			//判断token和appId是否存在
			if(StringHelper.isNotBlank(token) && StringHelper.isNotBlank(appId) ){
				//登录验证
				UsersMall usersMall =  this.getUsersByTokenAndAppId(token, appId);
				//判断是否登录成功
				if(usersMall!=null){
					if (StringHelper.isNotBlank(skuId) && StringHelper.isNotBlank(overallLevel)){
						
							/*spuFeedback.setSpuId(spuId);*/	 //spuId
						
							spuFeedback.setUserNickName(usersMall.getUserNickName()); //用户昵称
							spuFeedback.setUserHeadImg(usersMall.getUserHeadImg()); //用户头像
							spuFeedback.setUserId(usersMall.getId());//用户id
				
							spuFeedback.setReorder(reorder);
							spuFeedback.setSkuId(skuId); //skuId
							spuFeedback.setContent(content); //评价内容(至少5字以上)
							spuFeedback.setImages(images); //图片URL
							spuFeedback.setOverallLevel(overallLevel);//总评级数
							spuFeedback.setWuliuLevel(wuliuLevel); //物流评价
							spuFeedback.setDescLevel(descLevel); //描述评价
							spuFeedback.setServerLevel(serverLevel); //服务评价
							
							GoodsSku goodsSkuflag = goodsSkuService.getSkuBySkuId(skuId);//根据skuId 获取spuId
							spuFeedback.setSpuId(goodsSkuflag.getSpuId()); //spuId
							
							result = puFeedbackService.add(spuFeedback);//添加方法
							
							String overallLevel1 =  spuFeedback.getOverallLevel();//总评价数
							
							GoodsSku goodsSku = goodsSkuService.getSkuBySkuId(skuId);//根据skuId查询sku
							
							GoodsSpu goodsSpu = new GoodsSpu();
							
							if(null != goodsSku){
								if(StringHelper.isNotBlank(goodsSku.getSpuId())){
									goodsSpu = goodsSpuService.get(goodsSku.getSpuId()); //获取spu商品
								}
							}else{
								errorCode = OBJECT_NULL_ERROR_MSG;//对象获取失败
								errorMsg = OBJECT_ERROR_MSG;
							}
							int TotalFeedback = 0 ;
							int TotalgoodsFeedback = 0 ;
							
							if(null != goodsSpu){
								 TotalFeedback =  goodsSpu.getTotalFeedback(); //总评价数
								 TotalgoodsFeedback =  goodsSpu.getTotalGoodFeedback();//获取好评数
								 
								 goodsSpu.setTotalFeedback(NumericHelper.addInteger(TotalFeedback, 1));//总评价数+1
								//判断当前评价是否是好评
								if("5".equals(overallLevel1)){
									//总好评价数+1
									goodsSpu.setTotalGoodFeedback(NumericHelper.addInteger(TotalgoodsFeedback, 1));
								}
								result = goodsSpuService.edit(goodsSpu);
								//TODO 关闭订单
								this.ordersItemService.closeOrderItem(orderNo, skuId);
								errorCode = SUCCESS_ERROR_CODE;//请求成功
								errorMsg = SUCCESS_ERROR_MSG;
							}else{
								errorCode = OBJECT_NULL_ERROR_MSG;//对象获取失败
								errorMsg = OBJECT_ERROR_MSG;
							}
						}else{
							errorCode = PARAM_ERROR_CODE;//缺少参数
							errorMsg = PARAM_ERROR_MSG;
						}
				}else{
					errorCode = LOGIN_ERROR_CODE;
					errorMsg = LOGIN_ERROR_MSG; //用户未登入
				}
			}
		}catch (Exception e) {
			errorCode = SERVER_ERROR_CODE;
			errorMsg = SERVER_ERROR_MSG;
			logException("SpuFeedbackInterface", "add", e.toString(), "添加模板管理数据", e);
		}
		resultMap.put(CODE, errorCode);
		resultMap.put(MSG, errorMsg);
		return resultMap;
	}
	
	
	/**
	 * <p>
	 * Title:queryCondition
	 * </p>
	 * <p>
	 * Description:封装查询条件
	 * </p>
	 * @param jzdw
	 * @param jzdwExample
	 * @return
	 */
	private SpuFeedbackExample queryCondition(HttpServletRequest request, SpuFeedback spuFeedback) {
		// 参数处理
		SpuFeedbackExample example = new SpuFeedbackExample();
		// 按创建时间倒序
		example.setOrderByClause("create_time desc");
		// 按排列号顺序
		example.setOrderByClause("reorder asc");
		Criteria criteria = example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
		// 查询条件
		if (null !=  spuFeedback) {
			// 模糊查询
			if (StringHelper.isNotBlank(spuFeedback.getUserNickName())) {
				criteria.andUserNickNameEqualTo("%" + spuFeedback.getUserNickName() + "%");
			}
		}
		return example;
	}

}
